package controller

import (
	"github.com/gin-gonic/gin"
	"go-demo/common"
	"go-demo/model"
	"go-demo/service"
	"net/http"
	"strconv"
)

type UserListResponse struct {
	model.Response
	UserList []model.User `json:"user_list"`
}

// RelationAction no practical effect, just check if token is valid
// 关注操作
func RelationAction(c *gin.Context) {
	token := c.Query("token")
	actionType := c.Query("action_type")
	toUserId, _ := strconv.ParseInt(c.Query("to_user_id"), 10, 64)
	follow := service.ExecRelationAction(token, toUserId, actionType)
	if follow != nil {
		c.JSON(http.StatusOK, model.Response{StatusCode: common.SUCCESS, StatusMsg: "关注成功"})
	} else {
		c.JSON(http.StatusOK, model.Response{StatusCode: common.ERROR, StatusMsg: "关注失败"})
	}
}

// 获取关注列表
func FollowList(c *gin.Context) {
	token := c.Query("token")
	userId, _ := strconv.ParseInt(c.Query("user_id"), 10, 64)
	userList := service.GetFollowList(token, userId)
	if userList != nil {
		c.JSON(http.StatusOK, UserListResponse{
			Response: model.Response{
				StatusCode: common.SUCCESS,
				StatusMsg:  "获取关注列表成功",
			},
			UserList: userList.([]model.User),
		})
	} else {
		c.JSON(http.StatusOK, model.Response{StatusCode: common.ERROR, StatusMsg: "获取关注列表失败"})
	}
}

// 获取粉丝列表
func FollowerList(c *gin.Context) {
	token := c.Query("token")
	userId, _ := strconv.ParseInt(c.Query("user_id"), 10, 64)

	userList := service.GetFollowerList(token, userId)
	if userList != nil {
		c.JSON(http.StatusOK, UserListResponse{
			Response: model.Response{
				StatusCode: common.SUCCESS,
				StatusMsg:  "获取粉丝列表成功",
			},
			UserList: userList.([]model.User),
		})
	} else {
		c.JSON(http.StatusOK, model.Response{StatusCode: common.ERROR, StatusMsg: "获取粉丝列表失败"})
	}
}

// 获取好友列表
func FriendList(c *gin.Context) {
	token := c.Query("token")
	userId, _ := strconv.ParseInt(c.Query("user_id"), 10, 64)
	userList := service.GetFriendList(token, userId)
	if userList != nil {
		c.JSON(http.StatusOK, UserListResponse{
			Response: model.Response{
				StatusCode: common.SUCCESS,
				StatusMsg:  "获取好友列表成功",
			},
			UserList: userList.([]model.User),
		})
	} else {
		c.JSON(http.StatusOK, model.Response{StatusCode: common.ERROR, StatusMsg: "获取好友列表失败"})
	}
}
